<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
require_once 'modules/content/models/ContentCategory.php';
class Models_Comment extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'comment_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'comment';
        return parent::__construct($config); 
    }
    public function getAllEnabledCommentById( $gid, $order = null)
    {
        $select = $this->select()
                ->where('enabled = 1')
                ->where('content_id=?',$gid)
                ->order($order)
                ->limit($count, $offset);

               
        return $this->fetchAll($select)->toArray();
    }
    public function getAllCommentsWithCategory($condition = array(), $order = null, $count = null, $offset = null)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('com' => $this->_name))
                ->join(array('con' => $this->_prefix . 'content'), 'com.content_id = con.scontent_id', array('content_category_id'=>'content_category_id'))
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $select->where($this->getAdapter()->quoteInto('com.name LIKE ?', "%{$condition['keyword']}%"));
        }
        if (null != @$condition['scontent_category_id']) {
            $select->where("cat.content_category_id = ?", $condition['content_category_id']);
        }
        if (null != @$condition['content_id']) {
            $select->where("con.scontent_id = ?", $condition['content_id']);
        }
        if (null != @$condition['userId']) {
            $select->where("p.user_id = ?", $condition['userId']);
        }
        
//        echo '<pre>'; print_r($select);die;
        return $this->fetchAll($select)->toArray();
    }
public function getAllComments($condition = array(), $order = null, $count = null, $offset = null)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('c' => $this->_name))
                ->join(array('cc' => $this->_prefix . 'content'), 'c.content_gid = cc.content_gid', array('title' => 'title','enabled' => 'c.enabled'))
                ->join(array('l' => $this->_prefix . 'lang'), 'c.lang_id = l.lang_id', array('lname' => 'name', 'lang_image'))
                ->where('cc.lang_id=?', Nine_Language::getCurrentLangId())
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $sql = "SELECT content_gid FROM {$this->_name} WHERE " . $this->getAdapter()->quoteInto('title LIKE ?', "%{$condition['keyword']}%");
            $ids = $this->_db->fetchAll($sql);
            if (empty($ids)) {
                return array();
            }
            $idString = '';
            foreach ($ids as $row) {
                $idString .= $row['content_gid'] . ',';
            }
            /**
             * Add to select object
             */
            $select->where('c.content_gid IN (' . trim($idString, ',') .')');
        }
        if (null != @$condition['content_gid']) {
            
            $sql = "SELECT gid_string FROM {$this->_prefix}" . 'content' . " WHERE " . $this->getAdapter()->quoteInto('content_gid = ? ', $condition['content_gid']);
            $ids = @reset($this->_db->fetchAll($sql));
            if (null == $ids) {
                $ids = '0';
            }
            /**
             * Add to select object
             */
            $select->where('c.content_gid IN (' . trim($ids['gid_string'], ',') .')');
            
        }
        
       
        
        
        return $this->fetchAll($select)->toArray();
    }
    
    
}